
from rest_framework.viewsets import ModelViewSet

from .serializers import BookInfoSerializer
from .models import BookInfo


# class BookInfoViewSet(ModelViewSet):
#
#     queryset = BookInfo.objects.all()
#
#     serializer_class = BookInfoSerializer


from booktest.serializers import BookInfoSerializer, HeroInfoSerializer
from booktest.models import BookInfo, HeroInfo

"""序列化单个模型对象"""
# book = BookInfo.objects.get(id=2)
# serializer = BookInfoSerializer(instance=book)
# serializer.data


"""序列化查询集"""
# 如果要序列化的是一个查询集时，要指定many=True
# book = BookInfo.objects.all()
# serializer = BookInfoSerializer(instance=book, many=True)
# serializer.data

"""关联序列化"""
# hero = HeroInfo.objects.get(id=1)
# serializer = HeroInfoSerializer(instance=hero)
# serializer.data


"""反序列化"""
data_dict = {
    'btitle': '水浒传django',
    'bpub_date': '1990-11-11'
}

book = BookInfo.objects.get(id=12)

serializer = BookInfoSerializer(data=data_dict)

serializer.is_valid(raise_exception=True)

# serializer.validated_data

serializer.save()